package jsyf.oa.dao.email;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.BaseUtils;
import jsyf.oa.entity.attach.AttachmentEntity;
import jsyf.oa.entity.email.EmailMainEntity;
import jsyf.oa.entity.email.EmailReceiverEntity;
import jsyf.oa.entity.email.InboxEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/25 0025.
 */
@Repository
public class InboxDao extends BaseDao {
    public InboxDao() {
        super.iBaseEntity = new InboxEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, InboxEntity.class);
    }

    public EmailMainEntity getByEmailDetail(String emailId) {
        StringBuffer sql = new StringBuffer();
        sql.append("  SELECT \n" +
                " email.ID AS id, \n" +
                " email. SUBJECT AS subject, \n" +
                " email.CONTENT AS content, \n" +
                " email.CREATE_USER_ID AS createUserId, \n" +
                " email.CREATE_TIME AS createTime, \n" +
                " email.E_TYPE AS etype, \n" +
                " email.DATA_ID AS dataId, \n" +
                " tUser.NAME AS sender \n" +
                " FROM \n" +
                " t_sys_email AS email \n" +
                " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                " WHERE 1 = 1 \n" +
                " AND email.ID = ? \n" +
                "UNION ALL\n" +
                " SELECT \n" +
                " email.ID AS id, \n" +
                " email. SUBJECT AS subject, \n" +
                " email.CONTENT AS content, \n" +
                " email.CREATE_USER_ID AS createUserId, \n" +
                " email.CREATE_TIME AS createTime, \n" +
                " email.E_TYPE AS etype, \n" +
                " email.DATA_ID AS dataId, \n" +
                " tUser.NAME AS sender \n" +
                " FROM \n" +
                " t_sys_email_all AS email \n" +
                " LEFT JOIN T_SYS_USER  AS tUser ON email.CREATE_USER_ID=tUser.id \n" +
                " WHERE 1 = 1 \n" +
                " AND email.ID = ? \n \n");

        Object[] params = {emailId,emailId};
        // 查询结果
        JSONObject objResult = super.queryForJsonObject(sql.toString(), params);

        return JSON.toJavaObject(objResult, EmailMainEntity.class);
    }

    public List<JSONObject> getViewAllByPage(String sbCondition,  int requestPage, int pageSize) {
        StringBuffer sql = new StringBuffer();
        sql.append("select DISTINCT * from ( SELECT\n" +
                "\t*\n" +
                "FROM\n" +
                "\tv_sys_email_all\n" +
                "WHERE\n" +
                "\t1 = 1\n");
        sql.append(sbCondition);
        sql.append("AND draft = 0 \n" +
                "UNION ALL\n" +
                "\tSELECT\n" +
                "\t\t*\n" +
                "\tFROM\n" +
                "\t\tv_sys_email\n" +
                "\tWHERE\n" +
                "\t\t1 = 1\n");
        sql.append(sbCondition);
        sql.append(
                "\tAND draft = 0\n" +
                        "\tORDER BY\n" +
                        "\t\tcreateTime DESC) as email");
        super.appendPageSql(sql, requestPage, pageSize);
        List<JSONObject> jsonList = super.queryForJsonList(sql.toString());
        return jsonList;
    }
    public Integer getViewAllByCount(String sbCondition) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select sum(tmpcount) from ( \n" +
                "SELECT\n" +
                "\tcount(*) as tmpcount\n" +
                "FROM\n" +
                "\tv_sys_email_all\n" +
                "WHERE\n" +
                "\t1 = 1");
        sql.append(sbCondition);
        sql.append(" AND draft = 0\n" +
                "UNION ALL\n" +
                "\tSELECT\n" +
                "\t\tcount(*) as tmpcount\n" +
                "\tFROM\n" +
                "\t\tv_sys_email\n" +
                "\tWHERE\n" +
                "\t\t1 = 1");
        sql.append(sbCondition);
        sql.append(
                "\tAND draft = 0 ) email");
        String count = super.queryForString(sql.toString());
        if (count == null) {
            count = "0";
        }
        return Integer.parseInt(count);
    }
    public List<EmailReceiverEntity> findByEmailReceiver(String emailId, String userType) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select DISTINCT * from ( SELECT \n");
        sql.append(" receiver.USER_ID AS userId, \n");
        sql.append(" receiver.USER_NAME AS userName\n");
        sql.append(" FROM \n");
        sql.append(" T_SYS_EMAIL_RECEIVER AS receiver \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND receiver.EMAIL_ID = ? AND receiver.USER_TYPE = ? \n");
        sql.append(" UNION ALL SELECT DISTINCT receiver.USER_ID AS userId,receiver.USER_NAME AS userName FROM  T_SYS_EMAIL_RECEIVER_ALL AS receiver " +
                " WHERE 1=1 AND receiver.EMAIL_ID = ? AND receiver.USER_TYPE = ? ) as email");
        Object[] params = {emailId, userType,emailId,userType};

        // 查询结果
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);
        List<EmailReceiverEntity> list = new ArrayList<EmailReceiverEntity>();
        for (int i = 0; i < listResult.size(); i++) {
            EmailReceiverEntity m = JSON.toJavaObject(listResult.get(i), EmailReceiverEntity.class);
            list.add(m);
        }
        return list;
    }


    public List<AttachmentEntity> findByAttaches(String emailId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT \n");
        sql.append(" attach.ID AS id, \n");
        sql.append(" attach.FILE_NAME AS name, \n");
        sql.append(" attach.FILE_URL AS url \n");
        sql.append(" FROM \n");
        sql.append(" T_ATTACH_MODULE AS attach_module \n");
        sql.append(" LEFT JOIN T_ATTACHMENT AS attach ON attach_module.ATTACH_ID = attach.ID \n");
        sql.append(" WHERE 1=1 \n");
        sql.append(" AND attach_module.MODULE_ID = ? \n");
        Object[] params = {emailId};

        // 查询结果
        List<JSONObject> listResult = super.queryForJsonList(sql.toString(), params);
        List<AttachmentEntity> list = new ArrayList<AttachmentEntity>();
        for (int i = 0; i < listResult.size(); i++) {
            AttachmentEntity m = JSON.toJavaObject(listResult.get(i), AttachmentEntity.class);
            list.add(m);
        }
        return list;
    }


    public Integer emailReader(String emailId, String userId) {
        JSONObject obj = new JSONObject();
        obj.put("READER", 1);
        obj.put("READ_OPEN_TIME", getCurrentTime());
        JSONObject ID = new JSONObject();
        ID.put("USER_ID", userId);
        ID.put("EMAIL_ID", emailId);
        return  super.update("T_SYS_EMAIL_RECEIVER", obj, ID);
    }
    public Integer emailReaderAll(String emailId, String userId) {
        JSONObject obj = new JSONObject();
        obj.put("READER", 1);
        obj.put("READ_OPEN_TIME", getCurrentTime());
        JSONObject ID = new JSONObject();
        ID.put("USER_ID", userId);
        ID.put("EMAIL_ID", emailId);
       return super.update("T_SYS_EMAIL_RECEIVER_ALL", obj, ID);
    }
}
